<template>
	<view class="content">
		<view class="search-content">
			<u-search :placeholder="placeholderKeyword" v-model="keyword" :focus="true" :showAction="true" actionText="搜索" :animation="true" @search="clickSearch" @custom="clickSearch"></u-search>
		</view>
		<view class="product-content" :style="'min-height:'+(systemHeight-80)+'px'">
			<yn-list :lists="productList" type="product"></yn-list>
			<u-loadmore :status="status" />
		</view>
		
		<yn-tabbar currentTab='product'></yn-tabbar>
	</view>
</template>

<script>
	import API from '@/common/api.js';
	var app = getApp();
	export default {
		data() {
			return {
				baseinfo: [],
				minappinfo: [],
				keyword: '',
				placeholderKeyword: '搜索您需要的产品',
				productList: [],
				isLastPage: false,
				status: "nomore",
				page: 1,
				systemHeight: 400,
			}
		},
		onLoad(options) {
			this.baseinfo = uni.getStorageSync('baseinfo');
			this.minappinfo = uni.getStorageSync('minappinfo');
			
			if(options.keyword){
				this.clickSearch({
					search: options.keyword,
					page: this.page
				})
			}
			
			this.systemHeight = uni.getSystemInfoSync().windowHeight;
		},
		onShareAppMessage: function() {
			let title = this.minappinfo.tShareName ? this.minappinfo.tShareName : '云诺服务';
			let img = this.minappinfo.tLogo ? this.minappinfo.tLogo : '../../images/default.png'
		
			return {
				title: title,
				imageUrl: img,
				path: '/pages/index/index'
			}
		},
		onShareTimeline: function(res) {
			let title = this.minappinfo.tShareName ? this.minappinfo.tShareName : '云诺服务';
			let img = this.minappinfo.tLogo ? this.minappinfo.tLogo : '../../images/default.png'
			if (this.minappinfo.tShareDesc) {
				title = this.minappinfo.tShareName + " - " + this.minappinfo.tShareDesc
			}
			return {
				title: title,
				imageUrl: img,
				path: '/pages/index/index'
			}
		},
		onReachBottom() {
			if (!this.isLastPage) {
				this.clickSearch({
					search: this.keyword,
					page: this.page
				})
		
			}
		},
		methods: {
			clickSearch: function(e) {
				this.keyword = e.search;
				this.page = e.page;
				this.isLastPage = true;
				this.status = "nomore";
				this.productList = [];
				this.getProducts({
					search: this.keyword,
					page: this.page
				});
			},
			getProducts: function(args) {
				API.getProducts(args).then(res => {
						if (res.data.data.length < 10 || res.data.data == [] || res.data.data == null) {
							this.isLastPage = true;
							this.status = "nomore";
						}
			
						let product = [];
						let arr = res.data.data;
						arr.forEach(function(i, index) {
							product[index] = i;
							if (i['tPhoto']) {
								product[index]['tPhoto'] = API.getApiUrl() + '/cos/img?key=' + i['tPhoto'];
							} else {
								product[index]['tPhoto'] = '/static/images/default.png';
							}
						})
			
						this.productList = this.productList.concat(product);
			
						this.page = this.page + 1;
						console.log(this.page);
						this.isLoading = false;
					})
					.catch(err => {
						console.log(err);
					})
			}
		},
	}
</script>

<style lang="scss" scoped>
	.search-content{
		padding: 0rpx 20rpx;
	}
</style>
